﻿using Cinchoo.Core;
using Cinchoo.Core.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChoWorkflowServer
{
    [ChoCommandLineArgBuilder(OptionalCommand = true)]
    public class ChoWFServerCmdLineArgBuilder : ChoCommandLineArgBuilder
    {
        [ChoCommandLineArgBuilderCommand("RUN_WF", typeof(ChoWFExecutionLineArgsObject), Order = 0)]
        public ChoWFServerCmdLineArgBuilder()
        {
        }

        public static bool RunAsServer
        {
            get
            {
                ChoWFServerCmdLineArgBuilder bldr = new ChoWFServerCmdLineArgBuilder();
                return bldr != null && !(bldr.CommandLineArgObject is ChoWFExecutionLineArgsObject);
            }
        }

        public static ChoWFExecutionLineArgsObject WFExecutionLineArgsObject
        {
            get
            {
                ChoWFServerCmdLineArgBuilder bldr = new ChoWFServerCmdLineArgBuilder();
                return bldr != null ? bldr.CommandLineArgObject as ChoWFExecutionLineArgsObject : null;
            }
        }
    }

    [ChoCommandLineArgObject]
    public class ChoWFExecutionLineArgsObject : ChoCommandLineArgObject
    {
        [ChoCommandLineArg("t", Description = "Task Name.", Order = 0, IsRequired = true)]
        public string TaskName;

        [ChoCommandLineArg("s", Description = "Schedule Name.", Order = 1)]
        public string ScheduleName;

        [ChoCommandLineArg("w", Description = "Workflow file path.", Order = 2)]
        public string WFFilePath;

        [ChoCommandLineArg("b", Description = "Batch Id. If missing, will be assigned one.", Order = 3)]
        public int BatchId;

        [ChoCommandLineArg("u", Description = "User Name. If missing, active user account will be used.", Order = 4)]
        public string UserName;

        [ChoCommandLineArg("r", Description = "Resume the workflow.", Order = 5)]
        public bool ResumeTask;

        [ChoCommandLineArg("p", Description = "Optional Workflow parameters.", Order = 6)]
        public string WFParams;

        public string JobName
        {
            get { return "{0}_{1}".FormatString(TaskName, ScheduleName); }
        }

        protected override bool OnBeforeCommandLineArgLoaded(string memberName, ref string value, object defaultValue, object fallbackValue)
        {
            return base.OnBeforeCommandLineArgLoaded(memberName, ref value, defaultValue, fallbackValue);
        }

        protected override void OnAfterCommandLineArgObjectLoaded(string[] commandLineArgs)
        {
            if (ScheduleName.IsNullOrWhiteSpace())
                ScheduleName = "S1";
            if (ResumeTask && BatchId == 0)
                throw new ApplicationException("Invalid batch id.");
            if (BatchId == 0)
                BatchId = ChoRandom.NextRandom();

            if (UserName.IsNullOrWhiteSpace()) UserName = Environment.UserName;

            base.OnAfterCommandLineArgObjectLoaded(commandLineArgs);
        }
    }
}
